/*
 * @Author: HZK
 * @Date: 2020-12-15 16:25:34
 * @LastEditTime: 2020-12-22 14:23:58
 * @LastEditors: hanfengmi
 * @Description: 埋点
 */

let osFn = {
  detect: function () {
    let ret = {
      browser: this.search(this.data.bs),
      version: this.search(navigator.userAgent),
      os: this.search(this.data.os)
    }
    if (ret.os === 'Linux') {
      let distros = ['CentOS', 'Debian', 'Fedora', 'Gentoo', 'Mandriva', 'Mageia', 'Red Hat', 'Slackware', 'SUSE', 'Turbolinux', 'Ubuntu']
      for (let i = 0; i < distros.length; i++) {
        if (navigator.userAgent.toLowerCase().match(distros[i].toLowerCase())) {
          ret.distro = distros[i]
          break
        }
      }
    }
    return ret
  },
  search: function (data) {
    if (typeof data === 'object') {
      for (let i = 0; i < data.length; i++) {
        let dataString = data[i].string,
          dataProp = data[i].prop
        this.version_string = data[i].versionSearch || data[i].identity
        if (dataString) {
          if (-1 !== dataString.indexOf(data[i].subString)) {
            return data[i].identity
          }
        } else if (dataProp) {
          return data[i].identity
        }
      }
    } else {
      let index = data.indexOf(this.version_string)
      if (index === -1) return
      return parseFloat(data.substr(index + this.version_string.length + 1))
    }
  },
  data: {
    bs: [
      { string: navigator.userAgent, subString: 'Chrome', identity: 'Chrome' },
      { string: navigator.userAgent, subString: 'OmniWeb', versionSearch: 'OmniWeb/', identity: 'OmniWeb' },
      { string: navigator.vendor, subString: 'Apple', identity: 'Safari', versionSearch: 'Version' },
      { prop: window.opera, identity: 'Opera', versionSearch: 'Version' },
      { string: navigator.vendor, subString: 'iCab', identity: 'iCab' },
      { string: navigator.vendor, subString: 'KDE', identity: 'Konqueror' },
      { string: navigator.userAgent, subString: 'Firefox', identity: 'Firefox' },
      { string: navigator.vendor, subString: 'Camino', identity: 'Camino' },
      { string: navigator.userAgent, subString: 'Netscape', identity: 'Netscape' },
      { string: navigator.userAgent, subString: 'MSIE', identity: 'Explorer', versionSearch: 'MSIE' },
      { string: navigator.userAgent, subString: 'Gecko', identity: 'Mozilla', versionSearch: 'rv' },
      { string: navigator.userAgent, subString: 'Mozilla', identity: 'Netscape', versionSearch: 'Mozilla' }
    ],
    os: [
      { string: navigator.platform, subString: 'Win', identity: 'Windows' },
      { string: navigator.platform, subString: 'Mac', identity: 'Mac' },
      { string: navigator.userAgent, subString: 'iPhone', identity: 'iPhone/iPod' },
      { string: navigator.userAgent, subString: 'iPad', identity: 'iPad' },
      { string: navigator.userAgent, subString: 'Android', identity: 'Android' },
      { string: navigator.platform, subString: 'Linux', identity: 'Linux' }
    ]
  }
}

function getBrowserInfo () {
  try{
    let a1 = osFn.detect()
    return '&os=' + a1.os + '&bs=' + a1.browser + '/' + a1.version
  }catch{
    return '&url=' + escape(window.location.href)
  }
}
function getScrollHeight () {
  return '&sh=' + Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
}
function getMainEnvInfoStr () {
  let result = ''
  let c1 = '-',
    b1 = '-',
    d1 = '-',
    e1 = '-',
    o1 = 0
  if(window.screen) {
    c1 = window.screen.width + 'x' + window.screen.height
    b1 = (window.screen.colorDepth || 0) + '-bit'
  };
  window.navigator.language ? d1 = window.navigator.language.toLowerCase() : window.navigator.browserLanguage && (d1 = window.navigator.browserLanguage.toLowerCase())
  o1 = window.navigator.javaEnabled() ? 1 : 0
  e1 = -((new Date()).getTimezoneOffset()) / 60
  result = '&scr=' + c1 + '&scl=' + b1 + '&lg=' + d1 + '&jv=' + o1 + '&tz=' + e1
  return result
}
// console.log(`${getBrowserInfo()}${getScrollHeight()}${getMainEnvInfoStr()}`)

export default `${getBrowserInfo()}${getScrollHeight()}${getMainEnvInfoStr()}`